//
//  ZZMusicMoreDuoseSelctController.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/10/3.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ZZMusicMoreDuoseSelctController.h"
#import "ZZMusicMoreLightTool.h"
#import "ZZLightViewGroupModel.h"
#import "ZZLightViewCellModel.h"
#import "ZZLightViewCell.h"
#import "ZZLightViewGroupModel.h"
#import "ZZMusicStyleVC.h"

#define kZZMusicMoreDuoseMoreCellIndetier @"ZZMusicMoreDuoseMoreCellIndetier"
@interface ZZMusicMoreDuoseSelctController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flow;

@property (nonatomic, strong)ZZLightViewGroupModel *lightGroup;

@end

@implementation ZZMusicMoreDuoseSelctController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"选择颜色的样式";
    titleLabel.font = [UIFont systemFontOfSize:17];
    titleLabel.textColor = [UIColor whiteColor];
    [titleLabel sizeToFit];
    self.navigationItem.titleView = titleLabel;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"icon_fanh"] forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(0, 0, 16, 16);
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchDown];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"新增" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftDidClick) forControlEvents:UIControlEventTouchDown];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    leftBtn.frame = CGRectMake(0, 0, 44, 44);
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZZLightViewCell" bundle:nil] forCellWithReuseIdentifier:kZZMusicMoreDuoseMoreCellIndetier];
    self.collectionView.backgroundColor = [UIColor blackColor];
    
    self.flow.itemSize = CGSizeMake(158 * Z_Scale, 189 * Z_Scale);
    self.flow.minimumInteritemSpacing = 10 * Z_Scale;
    self.flow.minimumLineSpacing = 10 * Z_Scale;
    self.flow.sectionInset = UIEdgeInsetsMake(0, 15 * Z_Scale, 0, 15 * Z_Scale);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - action

- (void)back {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)leftDidClick {
    
    // 新增
    
}

#pragma mark - collection D & D
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.lightGroup.models.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZZLightViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kZZMusicMoreDuoseMoreCellIndetier forIndexPath:indexPath];
    Z_WEAK_SELF;
    cell.model = self.lightGroup.models[indexPath.row];
    cell.setBtnClick = ^{
        
        ZZMusicStyleVC *vc = [[ZZMusicStyleVC alloc] init];
        vc.model = self.lightGroup.models[indexPath.row];
        vc.updateData = ^(ZZLightViewCellModel *model) {
            
            [[ZZMusicMoreLightTool new] save];
            
            [weakSelf.collectionView reloadData];
        };
        [self.navigationController pushViewController:vc animated:YES];
    };
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    self.preModel.isSelect = NO;
    
    
    ZZLightViewCellModel *model = self.lightGroup.models[indexPath.row];
    model.isSelect = YES;
    self.preModel = model;
    if (self.outPutModel) {
        
        self.outPutModel(model);
    }
    
    [[ZZMusicMoreLightTool new] save];
}


#pragma mark - lightGroup
- (ZZLightViewGroupModel *)lightGroup {
    
    if (!_lightGroup) {
        
        _lightGroup = [[ZZMusicMoreLightTool new] lightGroup];
    }
    return _lightGroup;
}


@end
